/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jLogon.java
 *
 * Created on 21/05/2010, 20:44:52
 */

package sgfi;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jdesktop.application.SingleFrameApplication;
import sgfi.Classes.ClassConecta;
import sgfi.Classes.Usuario;



/**
 *
 * @author Vitiazze
 */
public class jLogon extends javax.swing.JFrame {

    /** Creates new form jLogon */
    public jLogon(SingleFrameApplication app) {
        initComponents();
        this.getRootPane().setDefaultButton(okjButton);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        senhajPasswordField = new javax.swing.JPasswordField();
        conexaojButton = new javax.swing.JButton();
        okjButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        usuariojTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sgfi.SGFIApp.class).getContext().getResourceMap(jLogon.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(100, 100));
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jDesktopPane1.setBorder(javax.swing.BorderFactory.createMatteBorder(200, 200, 200, 200, resourceMap.getIcon("jDesktopPane1.border.tileIcon"))); // NOI18N
        jDesktopPane1.setToolTipText(resourceMap.getString("jDesktopPane1.toolTipText")); // NOI18N
        jDesktopPane1.setName("jDesktopPane1"); // NOI18N
        jDesktopPane1.setPreferredSize(new java.awt.Dimension(492, 258));

        senhajPasswordField.setFont(resourceMap.getFont("senhajPasswordField.font")); // NOI18N
        senhajPasswordField.setText(resourceMap.getString("senhajPasswordField.text")); // NOI18N
        senhajPasswordField.setBorder(null);
        senhajPasswordField.setName("senhajPasswordField"); // NOI18N
        senhajPasswordField.setOpaque(false);
        senhajPasswordField.setBounds(260, 130, 200, 40);
        jDesktopPane1.add(senhajPasswordField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        conexaojButton.setIcon(resourceMap.getIcon("conexaojButton.icon")); // NOI18N
        conexaojButton.setText(resourceMap.getString("conexaojButton.text")); // NOI18N
        conexaojButton.setContentAreaFilled(false);
        conexaojButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        conexaojButton.setName("conexaojButton"); // NOI18N
        conexaojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conexaojButtonActionPerformed(evt);
            }
        });
        conexaojButton.setBounds(20, 220, 105, 30);
        jDesktopPane1.add(conexaojButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        okjButton.setFont(resourceMap.getFont("okjButton.font")); // NOI18N
        okjButton.setIcon(resourceMap.getIcon("okjButton.icon")); // NOI18N
        okjButton.setText(resourceMap.getString("okjButton.text")); // NOI18N
        okjButton.setContentAreaFilled(false);
        okjButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        okjButton.setName("okjButton"); // NOI18N
        okjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okjButtonActionPerformed(evt);
            }
        });
        okjButton.setBounds(260, 190, 100, 31);
        jDesktopPane1.add(okjButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(380, 190, 60, 30);
        jDesktopPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        usuariojTextField.setFont(resourceMap.getFont("usuariojTextField.font")); // NOI18N
        usuariojTextField.setBorder(null);
        usuariojTextField.setName("usuariojTextField"); // NOI18N
        usuariojTextField.setOpaque(false);
        usuariojTextField.setBounds(260, 90, 200, 29);
        jDesktopPane1.add(usuariojTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
public ClassConecta conexao = new ClassConecta();

    private JDialog aboutBox;

    private void okjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okjButtonActionPerformed
        // TODO add your handling code here:
 this.setAlwaysOnTop(false);

        if ( (usuariojTextField.getText().length() == 0) || (senhajPasswordField.getText().length() == 0) ){
          JOptionPane.showMessageDialog(null,"Os campos de usuário e senha são obrigatórios.","ATENÇÃO",JOptionPane.WARNING_MESSAGE);
          return;
        }

        //Guardar preferencia de dominio
        Preferences prefs = Preferences.userRoot().node(""); //this.getClass().getName()
        //Definir classe conecta
        ClassConecta.url                = "jdbc:mysql://"+ prefs.get("ipMySQL", "127.0.0.1") +":"+ prefs.get("portaMySQL", "3306") +"/"+prefs.get("bancoMySQL", "apolo");
        ClassConecta.user               = prefs.get("usuarioMySQL", "root");
        ClassConecta.pwd                = prefs.get("senhaMySQL", "" );


        ClassConecta.conecta();

        //Consultar usuário e senha
        Usuario oUsuario = new Usuario();
        oUsuario.setUsuario(usuariojTextField.getText());
        String senha = new String(senhajPasswordField.getPassword());
        oUsuario.setSenha(senha);

        try {
            ResultSet rs = oUsuario.getAutenticar(conexao);
            //Se autenticar abrir tela principal
            if (rs.getRow() == 1){
              //JApoloApp.launch(JApoloApp.class, null);


              //JApoloView oForm = new JApoloView(JApoloApp.getApplication());
              //JApoloApp.getApplication().show(oForm);

        SGFIView main = new SGFIView(SGFIApp.getApplication());

        //Definir o status
        //main.setStatus();


        JFrame frame = main.getFrame();
        frame.setLocationRelativeTo(null);
        frame.pack();

        //Definindo o Tamanho do Padrão do Formulario de Acordo com Tamanho da Tela
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        //frame.setMinimumSize(new Dimension(800, 600));
        frame.setMinimumSize(new Dimension((screenSize.width - 50), (screenSize.height - 100)));
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        //Quando restaurar nãõ va para o canto direito e sim para o esquerdo.
//        frame.setLocation(10, 10);
        

        SGFIApp.getApplication().show(main);
//        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);


              this.dispose();
            }else{
              usuariojTextField.setText("");
              senhajPasswordField.setText("");
              JOptionPane.showMessageDialog(null, "O usuário e senha digitados não conferem, favor tente novamente.", "Aviso!", JOptionPane.WARNING_MESSAGE);
              return;
            }



        } catch (SQLException ex) {
            Logger.getLogger(jLogon.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        



    }//GEN-LAST:event_okjButtonActionPerformed

    private void conexaojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_conexaojButtonActionPerformed
        // TODO add your handling code here:
        jFConexao oFormConexao = new jFConexao();
        oFormConexao.pack();
        oFormConexao.setModal(true);
        oFormConexao.setVisible(true);
}//GEN-LAST:event_conexaojButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.setLocationRelativeTo(null);
        usuariojTextField.grabFocus();
        senhajPasswordField.setText("");

        //recuperar preferencia
//        Preferences prefs = Preferences.userRoot().node(""); //this.getClass().getName()

        //Recuperar parametros
        //Para usar os parâmetros é necessário que não haja preferencias armazendas
//        if ( (SGFIApp.ipMySQL.length() > 0) && ( prefs.get("ipMySQL", "" ).length() == 0 )  ){
//
//          //Definir classe conecta
//          prefs.put("ipMySQL", SGFIApp.ipMySQL );
//          prefs.put("usuarioMySQL", SGFIApp.usuarioBancoMySQL );
//          prefs.put("senhaMySQL", SGFIApp.senhaBancoMySQL );
//          prefs.put("portaMySQL", SGFIApp.portaMySQL );
//          prefs.put("bancoMySQL", SGFIApp.bancoMySQL );
//
//        }
    }//GEN-LAST:event_formWindowOpened

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new jLogon(SGFIApp.getApplication()).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton conexaojButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JButton okjButton;
    private javax.swing.JPasswordField senhajPasswordField;
    private javax.swing.JTextField usuariojTextField;
    // End of variables declaration//GEN-END:variables

}
